const { app, BrowserWindow, clipboard, shell, globalShortcut } = require("electron");
const path = require('path')
const url = require("url");


function registerShortcut() {
    globalShortcut.register('CommandOrControl+Shift+I', () => {
        // 获取当前窗口
        BrowserWindow.getFocusedWindow().webContents.openDevTools();
    });
}

function createWindow () {
  const win = new BrowserWindow({
    width: 850,
    height: 630,
    minWidth: 850,
    minHeight: 630,
    webPreferences: {
      devTools: true,
      nodeIntegration: true,
      enableRemoteModule: true,
      webSecurity: false,
      nativeWindowOpen: false,
    }
  })

  win.loadURL(url.format({
    pathname: path.join(__dirname, "/index.html"),
    protocol: "file:", //协议名
    slashes: true //是否有双斜线
}));
//   win.loadFile('index.html')
//   win.loadURL('http://127.0.0.1:8080')

//   win.webContents.openDevTools();
//   win.webContents.on("before-input-event", (event, input) => {

//     // let contents = win.webContents
//     if ((input.meta || input.control) && input.shift && input.key.toLowerCase() === 'i') {
//         //contents.openDevTools();
//         BrowserWindow.getFocusedWindow().webContents.openDevTools();
//         event.preventDefault();
//         return;
//     }
//   });

//   win.webContents.on("new-window", (event, url, frameName, disposition, options, additionalFeatures) => {
    
//   });

}

// 忽略证书相关错误
app.commandLine.appendSwitch("ignore-certificate-errors");

app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');

// 禁用硬件加速
app.disableHardwareAcceleration();

// 爲了解決Windows下 notification 彈不出來的問題，原因不知
app.setAppUserModelId("com.easemob.webrtcdemo");

app.whenReady().then(() => {
  createWindow()

  registerShortcut();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('will-quit', () => {
    globalShortcut.unregisterAll();
})